home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / wordwrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  2.3 KB  |  94 lines

  1. /*
  2. **  WORDWRAP.C - Simple CRT word wrap demonstration routine
  3. **
  4. **  public domain by Robert Morgan
  5. */ 
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10.  
  11. int get_ln(int rmargin); 
  12. void clr_eol(const int curpos, const int pos); 
  13.  
  14. main() 
  15.       printf("Enter text.  Press CTRL-A to quit.\n"); 
  16.       while((get_ln(75)) != 0)      /* Change 75 to whatever number you */
  17.             ;                       /* wish to be the right margin      */ 
  18.       return 0;
  19.  
  20. void clr_eol(const int curpos, const int pos) 
  21.       int distance; 
  22.       int count; 
  23.  
  24.       distance = curpos - pos; 
  25.  
  26.       for (count = 1; count <= distance; count++) 
  27.             putch('\b'); 
  28.       for (count = 1; count <= distance; count++) 
  29.             putch(' '); 
  30.  
  31. int get_ln(int rmargin) 
  32.       char word[80]; 
  33.       static int wordpos = 0; 
  34.       static int curpos = 1; 
  35.       static int ch = 0; 
  36.       static int pos = 0; 
  37.  
  38.       word[wordpos] = '\0'; 
  39.  
  40.       while (ch != 1) 
  41.       { 
  42.             ch = getch(); 
  43.  
  44.             switch(ch) 
  45.             { 
  46.             case 1:
  47.                   return(0); 
  48.             case ' ':
  49.                   pos = curpos; 
  50.                   putch(' '); 
  51.                   curpos++; 
  52.                   wordpos = 0; 
  53.                   word[0] = '\0'; 
  54.                   break; 
  55.             case '\b':
  56.                   putch('\b'); 
  57.                   curpos--; 
  58.                   if (wordpos > 0) 
  59.                         wordpos--; 
  60.                   break; 
  61.             case '\r':
  62.                   puts("\r"); 
  63.                   wordpos = 0; 
  64.                   word[wordpos] = '\0'; 
  65.                   curpos = 1; 
  66.                   pos = 0; 
  67.                   break; 
  68.             default:
  69.                   putch(ch); 
  70.                   word[wordpos] = (char)ch; 
  71.                   curpos++; 
  72.                   wordpos++; 
  73.                   break; 
  74.             } 
  75.  
  76.             if(curpos == rmargin) 
  77.             { 
  78.                   word[wordpos] = '\0'; 
  79.                   clr_eol(curpos,pos); 
  80.                   wordpos = 0; 
  81.                   curpos = strlen(word); 
  82.                   pos = 0; 
  83.                   puts("\r"); 
  84.                   printf("%s",word); 
  85.             } 
  86.       } 
  87.       return -1;
  88.